<?php


namespace iflow\template\document\Tag;


use iflow\template\config\Config;
use iflow\template\document\abstracts\TagAbstract;
use iflow\template\document\Parser\DOMNodeParser;
use iflow\template\document\Parser\ParserHtml;

class FunctionTag extends TagAbstract {

    protected array $hiddenAttributes = [
        "action", "paramsname", "props", "tag"
    ];

    protected ?string $action = "";
    protected ?string $paramsName = "";

    public function parser(DOMNodeParser $node, Config $config, ParserHtml $parserHtml): static
    {
        parent::parser($node, $config, $parserHtml); // TODO: Change the autogenerated stub
        $this->action = $this->node -> getAttributes('action');
        $this->paramsName = $this->node -> getAttributes('paramsName');

        return $this->parserAttributes();
    }

    public function parserAttributes(): static
    {
        if (!$this->action) throw new \Exception("FunctionTag actionName for Empty");

        $props = $this -> props ? "...[{$this -> props}]" : "";

        $functionCode = "{$this->action}({$props})";

        if ($this->paramsName) {
            $this->html = "<?php \${$this -> paramsName} = $functionCode; ?>";
        } else {
            $this->html = "<?=$functionCode?>";
        }
        $this->html .= $this->traverseNodesToHtml();
        return $this;
    }
}